<?php
require "./Core.php";

define('rk', 'happy.');

class Happy
{
    private $data = [];
    private $redis;
    private static $ins;

    public static function getIns()
    {
        if (!self::$ins instanceof self) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    private function __construct()
    {
        $this->redis = new Redis();
        $this->redis->connect('127.0.0.1');
    }

    public function run($type, $groupCount, $range, $numberCount)
    {
        $rk = rk . date('Ymd') . '.' . time() . ".{$type}";
        echo "key={$rk}\n";
        $tmp = Core::run($groupCount, $range, $numberCount);
        foreach ($tmp as $list) {
            foreach ($list as $k => $v) {
                $this->redis->zIncrBy($rk, 1, $v);
            }
        }
        $this->data = $this->redis->zRevRange($rk, 0, $numberCount - 1, true);
        return $this->data;
    }
}

function getNumber($type, $gc, $start, $end, $numberCnt)
{

    $range = range($start, $end);
    $t = Happy::getIns();
    $result = $t->run($type, $gc, $range, $numberCnt);
    echo '<pre/>';
    var_export($result);
    return array_keys($result);
}

$gc = 1;
$b = getNumber('b', $gc, 1, 35, 5);
$a = getNumber('a', $gc, 1, 12, 2);


$result = "\n" . implode(' ', $b) . ' + ' . implode(' ', $a) . "\n";
echo $result;